<?php
/**
 * SQL Dump element.  Dumps out SQL log information 
 *
 * Infectamic: browser game (http://infectamic.goldzoneweb.info)
 * Copyright 2011, Loic Chardonnet
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

if (false === class_exists('ConnectionManager') || Configure::read('debug') < 2) {
    return false;
}

$noLogs = !isset($logs);
if (true === $noLogs) {
    $logs = array();
    $sources = ConnectionManager::sourceList();
    foreach ($sources as $source) {
        $db =& ConnectionManager::getDataSource($source);
        if (true === $db->isInterfaceSupported('getLog')) {
            $logs[$source] = $db->getLog();
        }
    }
}
?>

        <div class="container cake-sql-log">
            <?php if (true === $noLogs || true === isset($_forced_from_dbo_)): ?>
                <?php foreach ($logs as $source => $logInfo): ?>
                    <?php $tableId = 'cakeSqlLog_' . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)); ?>

                    <table class="sixteen columns" id="<?php echo $tableId; ?>" summary="Cake SQL Log" cellspacing="0" border="0">
                    <?php $text = $logInfo['count'] > 1 ? 'queries' : 'query'; ?>
                    <?php $caption = sprintf('(%s) %s %s took %s ms', $source, $logInfo['count'], $text, $logInfo['time']); ?>

                        <caption><?php echo $caption; ?></caption>
                        <thead>
                            <tr>
                                <th>Nr</th>
                                <th>Query</th>
                                <th>Error</th>
                                <th>Affected</th>
                                <th>Num. rows</th>
                                <th>Took (ms)</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($logInfo['log'] as $k => $i) : ?>
                                <tr>
                                    <td><?php echo ($k + 1); ?></td>
                                    <td><?php echo h($i['query']); ?></td>
                                    <td><?php echo $i['error']; ?></td>
                                    <td style="text-align: right"><?php echo $i['affected']; ?></td>
                                    <td style="text-align: right"><?php echo $i['numRows']; ?></td>
                                    <td style="text-align: right"><?php echo $i['took']; ?></td>
                                </tr>
                            <?php endforeach; ?>

                        </tbody>
                    </table>
                <?php endforeach; ?>
            <?php else: ?>

                <p>
                    Encountered unexpected <?php echo $logs; ?> cannot generate SQL log
                </p>
            <?php endif; ?>

        </div>
